<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="handleClose"
    title="栏目设置"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="栏目名称"
            prop="lmmc"
            style="width: 100%"
            :rules="[{ required: true, message: '请输入栏目名称', trigger: 'blue' }]"
          >
            <el-input v-model="form.lmmc" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否显示边框"
            prop="sfxsbk"
            :rules="{ required: true, message: '请选择是否显示边框', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.sfxsbk">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.sfxsbk === false" :span="12">
          <el-form-item
            label="是否显示阴影"
            prop="bkyy"
            :rules="{ required: true, message: '请选择是否显示阴影', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.bkyy">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否显示图标"
            prop="sfxs"
            :rules="{ required: true, message: '请选择是否显示图标', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.sfxs" style="width: 200px">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图标" prop="lmtb">
            <el-popover
              placement="bottom-start"
              width="450"
              trigger="click"
              :rules="{ required: true, message: '请选择图标', trigger: 'change' }"
              @show="$refs['iconSelect'].reset()"
            >
              <custom-icon ref="iconSelect" @selected="selectedIcon" />
              <el-input slot="reference" v-model="form.lmtb" style="width: 450px;" placeholder="点击选择图标" readonly>
                <i
                  v-if="form.lmtb"
                  slot="prefix"
                  :class="form.lmtb"
                  class="el-input__icon"
                  style="height: 32px;width: 16px;"
                />
                <i v-else slot="prefix" class="el-icon-search el-input__icon" />
              </el-input>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="模版" prop="data.id">
            <el-popover
              placement="bottom-start"
              width="450"
              trigger="click"
              :rules="{ required: true, message: '请选择模版', trigger: 'change' }"
              @show="$refs['tempSelect'].reset()"
            >
              <custom-temp ref="tempSelect" @selected="selectedTemp" />
              <el-input slot="reference" v-model="form.data.name" style="width: 450px" placeholder="点击选择模版" readonly />
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否显示更多"
            prop="isMore"
            :rules="{ required: true, message: '请选择是否显示更多', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.isMore">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.isMore === true" :span="12">
          <el-form-item
            label="打开方式"
            prop="dkfs"
            :rules="{ required: true, message: '请选择打开方式', trigger: 'change' }"
            style="width: 100%"
          >
            <el-radio-group v-model="form.dkfs">
              <el-radio
                v-for="item in openType"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.isMore === true && form.dkfs === 2" :span="24">
          <el-form-item
            label="打开地址"
            prop="path"
            :rules="{ required: true, message: '请输入打开地址', trigger: 'blur' }"
            style="width: 100%"
          >
            <el-input v-model="form.path" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item>
            <el-button type="text" @click="handleClose">取消</el-button>
            <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import CustomIcon from '@/components/CustomIcon'
import CustomTemp from '@/components/TempSelect/tempSelect'
const defaultForm = {
  id: null,
  lmmc: '',
  lmtb: '',
  sfxsbk: false,
  bkyy: false,
  sfxs: false,
  isMore: false,
  dkfs: 1,
  path: '',
  ls: 1,
  data: {},
  columnId: null
}
export default {
  name: 'PortalNewAddTemplate',
  components: { CustomIcon, CustomTemp },
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      status: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }],
      openType: [
        { id: 0, value: 1, label: '固定模版打开' },
        { id: 1, value: 2, label: '固定地址打开' }
      ],
      form: {
        id: null,
        lmmc: '',
        lmtb: '',
        sfxsbk: false,
        bkyy: false,
        sfxs: false,
        isMore: false,
        dkfs: 1,
        path: '',
        ls: 1,
        data: {},
        columnId: null
      }
    }
  },
  methods: {
    initObject(data) {
      if (data !== undefined && data !== null) {
        Object.assign(this.form, data)
      }
      this.dialogFormVisible = true
    },
    save() {
      this.loading = true
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$emit('save', this.form)
          this.handleClose()
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    selectedIcon(data) {
      this.form.lmtb = data
    },
    handleClose() {
      this.loading = false
      Object.assign(this.form, defaultForm)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    },
    selectedTemp(data) {
      this.form.data = data
    }
  }
}
</script>

<style scoped>

</style>
